package com.hopedove.returnyarnsystem.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.hopedove.base.BaseActivity;
import com.hopedove.net.ErrResponse;
import com.hopedove.net.OkResponse;
import com.hopedove.net.domain.Communication;
import com.hopedove.net.domain.FinishReturnYarnInfo;
import com.hopedove.returnyarnsystem.R;
import com.hopedove.utils.ToastUtil;
import com.hopedove.view.TipsDialogFragment;

public class ScanBarcodeActivity extends BaseActivity implements OnClickListener {

    private final int MAX_13 = 11;
    private EditText barCode;
    private TipsDialogFragment tipDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanbarcode);
        barCode = findViewById(R.id.barcode_et);
        barCode.setText("");
        findViewById(R.id.confirm_barcode_img).setOnClickListener(this);
        // 控制输入框只能接受9为数字，超过9位后，自动覆盖
        barCode.addTextChangedListener(new TextWatcher() {
            private int editEnd;
            private CharSequence temp;

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                temp = s;
            }

            @Override
            public void afterTextChanged(Editable s) {
                editEnd = barCode.getSelectionEnd();
                if (temp.length() > MAX_13) {
                    s.delete(0, editEnd - 1);
                    barCode.setText(s);
                    barCode.setSelection(editEnd + 1);
                }
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        barCode.setText("");
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.confirm_barcode_img) {

            if (barCode.getText().toString().equals("")) {
                ToastUtil.longToast(this, "请扫描条码");
                return;
            }

            // 弹出验证条码提示框
            View view = getLayoutInflater().inflate(R.layout.loading, null);
            TextView textView = view.findViewById(R.id.tip);
            String tipStr = "条码验证中...";
            textView.setText(tipStr);
            tipDialog = TipsDialogFragment.showNewInstance(this, view, false);
            finishReturnYarnInfo();
        }
    }


    /**
     * 获取布料信息
     */
    void finishReturnYarnInfo() {

        final String code = barCode.getText().toString();
        FinishReturnYarnInfo finishReturnYarnInfo = new FinishReturnYarnInfo(code);
        Communication.asyncRequest(finishReturnYarnInfo, r -> {
            TipsDialogFragment.dismissTipDialog(tipDialog);

            if (r instanceof OkResponse) {
                tipDialog = TipsDialogFragment.showNewInstance(this, R.layout.return_view_tips_type_2, false);
                uiHandler.postDelayed(() -> {
                    TipsDialogFragment.dismissTipDialog(tipDialog);
                    finish();
                }, 3000);
            } else if (r instanceof ErrResponse) {
                String err = ((ErrResponse) r).getErrText();
                if (TextUtils.isEmpty(err)) {
                    err = "退纱确认失败！";
                }
                ToastUtil.longToast(getBaseContext(), err);
            }
        });
    }

}
